
import {
  hotDynastyList,hotBookTypeList,hotPoetCateList
} from "../../utils/commondata.js";

import {
  request
} from "../../utils/request.js";
import regeneratorRuntime from '../../utils/runtime';


Page({

  /**
   * 页面的初始数据
   */
  data: {
    dynastyIndex:0,//朝代索引
    dynastyList:hotDynastyList(),//朝代列表
    authorList:[],
    bookTypeIndex:0,//典籍类型索引
    bookTypeList:hotBookTypeList(),//典籍类型列表
    bookList:[],
    poetCateIndex:0,//古诗分类索引
    poetCateList:hotPoetCateList(),//古诗分类列表
    poetList:[],
    hotAuthorList:{},//热门作者所有
    hotAncientBook:{},//热门古籍

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadPageHotData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.setData({
      dynastyList:hotDynastyList(),
      bookTypeList:hotBookTypeList(),
      poetCateList:hotPoetCateList(),
    });
    
    this.loadPagePoetryData(this.data.poetCateList[3].id,1);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let page = this.data.page +1;
    this.loadPagePoetryData(this.data.cateId,page);
  },

  /**
   * 朝代选择
   * @param {*} e 
   */
  dynastySelect(e){
    let {index} = e.currentTarget.dataset;
    this.loadAuthorList(index);
  },
  /**
   * 根据朝代加载作者
   * 
   * @param {*} index 
   */
  loadAuthorList(index){
    let dynasty = this.data.dynastyList[index];
    let authorList = this.data.hotAuthorList[dynasty];
    console.log(authorList);
    this.setData({
      dynastyIndex:index,
      authorList:authorList,
    });
  },
  /**
   * 典籍类型选择
   * @param {*} e 
   */
  bookTypeSelect(e){
    let {index} = e.currentTarget.dataset;
    this.loadBook(index);
  },
  /**
   * 加载古籍
   * @param {*} index 
   */
  loadBook(index){
    let bookType = this.data.bookTypeList[index];
    let bookList = this.data.hotAncientBook[bookType];
    console.log(bookList);
    this.setData({
      bookTypeIndex:index,
      bookList:bookList,
    });
  },
  /**
   * 诗词分类选择
   * @param {*} e 
   */
  poetCateSelect(e){
    let {index,id} = e.currentTarget.dataset;
    this.setData({
      poetCateIndex:index,
      poetList:[],
    });
    this.loadPagePoetryData(id,1);
  },
  handleAuthor(e){
    let {id} = e.currentTarget.dataset;
    wx.navigateTo({
      url:'/pages/author/detail/index?authorId=' + id,
    });
  },
  handleBook(e){
    let {id} = e.currentTarget.dataset;
    wx.navigateTo({
      url:'/pages/book/detail/index?bookId=' + id,
    });
  },
  handlePoet(e){
    let {id} = e.currentTarget.dataset;
    wx.navigateTo({
      url:'/pages/chapter/detail/index?chapterId=' + id,
    });
  },
  /**
   * 加载页面热门数据
   */
  async loadPageHotData(){
    const res = await request({
        url: "/home/queryHotData",
        method: "get"
      });
    if(res.code != 0){
      return;
    }
    this.setData({
      hotAuthorList:res.data.hotAuthorList,
      hotAncientBook:res.data.hotAncientBook
    });
    this.loadAuthorList(0);
    this.loadBook(0);
  },

  /**
   * 根据分类名称加载 古诗词内容
   * 
   * @param {*} cateName  分类名称
   * @param pageNo 页码
   */
  async loadPagePoetryData(cateId,pageNo){
    const res = await request({
      url: "/chapter/pageQueryByCategoryId?limit=10&categoryId=" + cateId + "&page=" + pageNo,
      method: "get"
    });
    if(res.code != 0){
      return;
    }
    this.setData({
      page:pageNo,
      cateId:cateId,
      poetList:[...this.data.poetList,...res.data.list],
    });
  },

})